<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/8/20
 * Time: 上午8:04
 */

namespace app\api\module;

use app\common\model\Club;
use app\common\model\DeveloperException;
use app\common\model\Pay;
use app\common\model\User;
use app\common\model\Order;
use app\common\model\Message;
use app\common\model\UserException;

class Active extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
            return true;
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return string
     */
    public function exec($interface)
    {
        switch ($interface)
        {
            /**
             * @api {post} / 【1001】链资源列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName active_list
             * @apiGroup 活动资源【Active】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             * @apiParam (dataGroup) {String} status 类别（-1:全部，1：进行中2：我参与的3：已结束）
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Active",
             *   "interface": "1001",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "page":"1",
             *     "status":"1",
             *     "page_size":"10"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} active_list 活动列表
             *
             * @apiSuccess (active_list-消息列表) {String} title 活动标题
             * @apiSuccess (active_list-消息列表) {String} time 活动时间
             * @apiSuccess (active_list-消息列表) {String} image 图片
             * @apiSuccess (active_list-消息列表) {String} id 活动id
             * @apiSuccess (active_list-消息列表) {String} address 活动地址
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "active_list": [
             *       {
             *              "id":"1",
             *             "title":"新闻标题",
             *             "time":"活动时间",
             *             "image":"https://www.baidu.com",
             *             "address":"沂蒙大道"
             *       },
             *       {
             *              "id":"1",
             *             "title":"新闻标题",
             *             "time":"活动时间",
             *             "image":"https://www.baidu.com",
             *             "address":"沂蒙大道"
             *       }
             *     ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "活动列表！"
             * }
             *
             */
            case H_ACTIVE_LIST:
                return $this->active_list();

            /**
             * @api {post} / 【1002】活动详情
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName active_info
             * @apiGroup 活动资源【Active】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} active_id 消息id
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Active",
             *   "interface": "1002",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "active_id": "10",
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {String} title biaoti
             * @apiSuccess (result_data-回执报文信息) {String} begintime 开始时间
             * @apiSuccess (result_data-回执报文信息) {String} endtime 结束时间
             * @apiSuccess (result_data-回执报文信息) {String} detail 内容详情
             * @apiSuccess (result_data-回执报文信息) {String} thumb 缩略图
             * @apiSuccess (result_data-回执报文信息) {String} coin h_币
             * @apiSuccess (result_data-回执报文信息) {String} money 现金
             *

             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *             "title":"新闻标题",
             *             "detail":"xinwenneiroing",
             *             "thumb":"https://www.baidu.com",
             *             "begintime":"545121805451",
             *             "endtime":"545121805451",
             *             "coin":"12",
             *             "money":"1001"
             *   },
             *   "result_code": "0000",
             *   "result_info": "活动详情"
             * }
             *
             */
            case H_ACTIVE_INFO:
                return $this->active_info();
            /**
             * @api {post} / 【1003】参与活动
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName active_buy
             * @apiGroup 活动资源【Active】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} active_id 活动id
             * @apiParam (dataGroup) {String} order_type 订单类型
             * @apiParam (dataGroup) {String} coin 我的h币
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Active",
             *   "interface": "1003",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "active_id": "10",
             *     "order_type":"1",
             *     "coin":"100"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             *
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *       "str":"121212121212123vcvc2v1cvc"
             *   },
             *   "result_code": "0000",
             *   "result_info": "提交成功！"
             * }
             *
             */
            case H_ACTIVE_BUY:
                return $this->active_buy();
            /**
             * @api {post} / 【1004】评价
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName active_evaluate
             * @apiGroup 活动资源【Active】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} order_id 订单id
             * @apiParam (dataGroup) {String} star_num 评价的星级
             * @apiParam (dataGroup) {String} content 评价的内容
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Active",
             *   "interface": "1004",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "order_id": "10",
             *     "star_num": "5",
             *     "content": "10"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             *
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *   },
             *   "result_code": "0000",
             *   "result_info": "评价成功！"
             * }
             *
             */
            case H_ACTIVE_EVALUATE:
                return $this->active_evaluate();
                /**
                 * @api {post} / 【1005】我的活动
                 * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
                 *
                 * @apiVersion 1.0.0
                 * @apiName my_active
                 * @apiGroup 活动资源【Active】
                 *
                 * @apiUse tnCommonParamTN
                 *
                 * @apiParam (dataGroup) {String} access_token Access Token
                 * @apiParam (dataGroup) {String} type 1参加的活动 2举办的活动
                 * @apiParam (dataGroup) {String} page 页码
                 * @apiParam (dataGroup) {String} page_size 每页几条
                 *
                 *
                 * @apiParamExample {json} 请求参数示例
                 *
                 * {
                 *   "version": "1.0.0",
                 *   "module": "Active",
                 *   "interface": "1005",
                 *   "data": {
                 *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz"，
                 *     "type":1，
                 *     "page":1,
                 *     "page_size":5
                 *   }
                 * }
                 *
                 * @apiSuccess (基础报文信息) {String} result_code 结果编码.
                 * @apiSuccess (基础报文信息) {String} result_info  结果信息.
                 * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
                 *
                 *
                 *
                 * @apiSuccessExample 成功示例
                 * {
                 *   "result_data": {
                 *      "active_list":[
                 *          {
                 *              "title":"迪拜旅游",
                 *              "time":"2018-08-20到21",
                 *              "address":"迪拜",
                 *              "image":"kljkljklj",
                 *           },
                 *          {
                 *              "title":"迪拜旅游",
                 *              "time":"2018-08-20到21",
                 *              "address":"迪拜",
                 *              "image":"kljkljklj",
                 *           }
                 *      ]
                 *   },
                 *   "result_code": "0000",
                 *   "result_info": "活动列表！"
                 * }
                 *
                 */
            case H_MY_ACTIVE:
                return $this->my_active();
            /**
             * @api {post} / 【1006】公告列表
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName notice_list
             * @apiGroup 活动资源【Active】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} type （1：平台通知，2：商会通知）
             * @apiParam (dataGroup) {String} page 第几页
             * @apiParam (dataGroup) {String} page_size 每页几个
             *
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Active",
             *   "interface": "1006",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *      "type":1,
             *      "page":"1",
             *     "page_size":"10"
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {Array} notice_list 活动列表
             *
             * @apiSuccess (notice_list-消息列表) {String} name 活动标题
             * @apiSuccess (notice_list-消息列表) {String} update_time 活动时间
             * @apiSuccess (notice_list-消息列表) {String} detail 内容
             * @apiSuccess (notice_list-消息列表) {String} id 活动id
             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *     "notice_list": [
             *       {
             *              "id":"1",
             *             "name":"新闻标题",
             *             "update_time":"活动时间",
             *             "detail":"https://www.baidu.com"
             *       },
             *       {
             *              "id":"1",
             *             "name":"新闻标题",
             *             "update_time":"活动时间",
             *             "detail":"https://www.baidu.com"
             *       }
             *     ]
             *   },
             *   "result_code": "0000",
             *   "result_info": "公告列表！"
             * }
             *
             */
            case H_NOTICE_LIST:
                return $this->notice_list();
            /**
             * @api {post} / 【1007】公告详情
             * @apiDescription <label class="label label-success" style="font-size: 18px;">已实现</label>
             *
             * @apiVersion 1.0.0
             * @apiName notice_info
             * @apiGroup 活动资源【Active】
             *
             * @apiUse tnCommonParamTN
             *
             * @apiParam (dataGroup) {String} access_token Access Token
             * @apiParam (dataGroup) {String} notice_id 消息id
             *
             * @apiParamExample {json} 请求参数示例
             *
             * {
             *   "version": "1.0.0",
             *   "module": "Active",
             *   "interface": "1007",
             *   "data": {
             *     "access_token": "l+ODeJqDuqErlzpGcecDYSHRhsChsSzUnbqp3mXH/uFR7SFwsSs6btHnmzWLdjq1F3n4OagyJaMErOwFKcRBN/6+wSlN+k6KravEH4KUunyOTKqhBXPayy+NZfHNyf3zLQ2SWl+21aqZhD6/yu8qM/cG4K1Vg1ebESXfEF8z93JD4cn28Ks1UsHmm62JPUcz",
             *     "notice_id": "10",
             *   }
             * }
             *
             * @apiSuccess (基础报文信息) {String} result_code 结果编码.
             * @apiSuccess (基础报文信息) {String} result_info  结果信息.
             * @apiSuccess (基础报文信息) {Object} result_data  回执报文数据.
             *
             * @apiSuccess (result_data-回执报文信息) {String} name biaoti
             * @apiSuccess (result_data-回执报文信息) {String} update_time 时间
             * @apiSuccess (result_data-回执报文信息) {String} detail 内容详情
             *

             *
             * @apiSuccessExample 成功示例
             * {
             *   "result_data": {
             *             "name":"新闻标题",
             *             "detail":"xinwenneiroing",
             *             "update_time":"545121805451"
             *   },
             *   "result_code": "0000",
             *   "result_info": "公告详情"
             * }
             *
             */
            case H_NOTICE_INFO:
                return $this->notice_info();
            default:
                send_developer_error("模块【活动资源】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    /*
     * 公告列表
     * */
    public function notice_list()
    {
        $access_token=$this->essentialParam('access_token');
        $type=$this->essentialParam('type');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');
        $user=User::userFromToken($access_token);
        if($type==1){
            $list=Message::noticeList('0',$page,$page_size);
        }else{
            $list=Message::noticeList($user['bussiness_id'],$page,$page_size);
        }

        $this->insertResponse('list',$list);
        return $this->response("公告列表");

    }

    /*
     * 公告详情
     * */
    public function notice_info()
    {
        $access_token=$this->essentialParam('access_token');
        $notice_id=$this->essentialParam('notice_id');

        $info=Message::noticeInfo($notice_id);
        $this->insertResponse('name',$info['name']);
        $this->insertResponse('update_time',$info['update_time']);
        $this->insertResponse('detail',$info['detail']);
        return $this->response("公告详情");

    }
    /*
     * 活动列表
     * */
    public function active_list()
    {
        $access_token=$this->essentialParam('access_token');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');
        $status=$this->essentialParam('status');
        $user=User::userFromToken($access_token);
        $bussiness=Club::bussinessInfoById($user['bussiness_id']);
        if(empty($bussiness['city']))
        {
            $bussiness['city']=182;
        }

        $list=Message::activityList($status,$bussiness['city'],$page,$page_size);
        $this->insertResponse('list',$list);
        return $this->response("活动列表！");

    }
    /*
   * 活动详情
   * */
    public function active_info()
    {
        $access_token=$this->essentialParam('access_token');
        $active_id=$this->essentialParam('active_id');

        $info=Message::activityInfo($active_id);

        $this->insertResponse('id',$info['id']);
         $this->insertResponse('union_id',$info['union_id']);
        $this->insertResponse('title',$info['title']);
        $this->insertResponse('detail',$info['detail']);
        $this->insertResponse('thumb',$info['thumb']);
        $this->insertResponse('address',$info['address']);
        $this->insertResponse('background',$info['background']);
        $this->insertResponse('begintime',$info['begintime']);
        $this->insertResponse('endtime',$info['endtime']);
        $this->insertResponse('coin',$info['coin']);
        $this->insertResponse('money',$info['money']);
        return $this->response("活动详情");

    }

    /**
     * 参与活动支付
     * @throws DeveloperException
     * @throws UserException
     * @throws \app\common\model\ServerException
     * @throws \app\common\model\TokenException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function active_buy()
    {
        $access_token=$this->essentialParam('access_token');
        $active_id=$this->essentialParam('active_id');
        $order_type=$this->essentialParam('order_type');
        $my_coin=$this->essentialParam('coin');
        $pay_type=$this->essentialParam('pay_type');

        $user=User::userFromToken($access_token);
        /*活动信息*/
        $info=Message::activityInfo($active_id);
        /*判断钱包是否够*/
        if($info['coin']>$my_coin)
        {
            throw new DeveloperException("您的H链资产不够，不能参与活动！");
        }
        /*生成订单*/
        $order=Order::establish($user,$info['money'],$info['coin'],$order_type,$info['uid'],$active_id,$pay_type,$info['union_id'],$info['money']);
        $body="会链参与活动";
        /*支付类型*/
        if($pay_type==1)
        {
            /*微信支付统一下单*/
            $payInfo=Pay::weixinPay($order['orderid'],$order['money'],$body);

        }else{
            /*支付宝支付统一下单*/
            $payInfo=Pay::aliPay($order['orderid'],$order['money'],$body);
        }

        $this->insertResponse('pay_info',$payInfo);
        $this->insertResponse('order_sn',$order['orderid']);

        return $this->response("参与活动");

    }

    /*
     * 评价订单
     * */
    public function active_evaluate()
    {
        $access_token=$this->essentialParam('access_token');
        $orderid=$this->essentialParam('order_id');
        $star_num=$this->essentialParam('star_num');
        $content=$this->essentialParam('content');

        $user=User::userFromToken($access_token);

        $info=Order::orderEvaluate($user,$orderid,$star_num,$content);

        return $this->response("评价成功");

    }

    /*
        * 我的活动列表
        * */
    public function my_active()
    {
        $access_token=$this->essentialParam('access_token');
        $type=$this->essentialParam('type');
        $page=$this->essentialParam('page');
        $page_size=$this->essentialParam('page_size');

        $user=User::userFromToken($access_token);

        $list=Message::myActive($user,$type,$page,$page_size);

        $this->insertResponse('list',$list);
        return $this->response("我的资源列表！");

    }


}